package com.xc.h05.service.weichen.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.Heartmark;
import com.xc.h05.entity.Mytest;
import com.xc.h05.entity.User;
import com.xc.h05.mapper.HeartmarkMapper;
import com.xc.h05.mapper.MytestMapper;
import com.xc.h05.mapper.UserMapper;
import com.xc.h05.service.weichen.HeartmarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class HeartmarkServiceImpl implements HeartmarkService {
    @Autowired
    private HeartmarkMapper heartmarkMapper;
    @Autowired
    private MytestMapper mytestMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Heartmark> findAll() {
        List<Heartmark> list = heartmarkMapper.findAll();
        return list;
    }
    // 根据**分页查询
    @Override
    public PageInfo<Heartmark> findByPage(int pageNum, int pageSize, Heartmark heartmark) {
        PageHelper.startPage(pageNum,pageSize);
        List<Heartmark> list = heartmarkMapper.findByPage(heartmark);
        for (Heartmark hm : list) {
            List<Integer> list1 = mytestMapper.findByTestId1(hm.getId());
            System.out.println(list1.size());
            if (list1.size()==0){
                hm.setRealTotal(0);
                hm.setUsers(null);
                continue;
            }
            List<User> list2 = userMapper.findByIds(list1);
//            System.out.println(list2);
            hm.setRealTotal(list1.size());
            hm.setUsers(list2);
        }
        PageInfo<Heartmark> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }

    @Override
    public Heartmark findById(long id) {
        Heartmark heartmark = heartmarkMapper.findById(id);
        return heartmark;
    }

    @Override
    public int add(Heartmark heartmark) {
        int add = heartmarkMapper.add(heartmark);
        return add;
    }

    @Override
    public int delete(int id) {
        int delete = heartmarkMapper.delete(id);
        return delete;
    }

    @Override
    public int deletes(List list) {
        int deletes = heartmarkMapper.deletes(list);
        return deletes;
    }

    @Override
    public int update(Heartmark heartmark) {
        int update = heartmarkMapper.update(heartmark);
        return update;
    }
}
